// miniprogram/pages/jiaruhuodong/jiaruhuodong.js
const DB = wx.cloud.database();
const _ = DB.command
Page({
    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        orderItem: '',
        collection: [],
        shoucangbtn: false,
        dianzanbtn: false,
        InputBottom: 0,
        openid: wx.getStorageSync('openid'),
        dianzanshu: 0,
        shoucangshu: 0
    },



    shoucang() {
        if (!this.data.shoucangbtn) {
            wx.showLoading({
                title: '加载中'
            })
            wx.cloud.callFunction({
                name: "update",
                data: {
                    action: "pushscid",
                    id: this.data.id
                }
            }).then(res => {
                console.log(res);
                wx.hideLoading({
                    success: (res) => {},
                })
                if (res.result.data.stats.updated === 1) {
                    wx.showToast({
                        title: '收藏成功',
                    })
                    this.setData({
                        shoucangbtn: true,
                        shoucangshu: this.data.shoucangshu + 1
                    })
                }
            })
        } else {
            wx.showLoading({
                title: '加载中'
            })
            wx.cloud.callFunction({
                name: "update",
                data: {
                    action: "pullscid",
                    id: this.data.id
                }
            }).then(res => {
                console.log(res);
                wx.hideLoading({
                    success: (res) => {},
                })
                if (res.result.data.stats.updated === 1) {
                    wx.showToast({
                        title: '取消成功',
                    })
                    this.setData({
                        shoucangbtn: false,
                        shoucangshu: this.data.shoucangshu - 1
                    })
                }
            })

        }
    },

    dianzan() {
        if (!this.data.dianzanbtn) {
            wx.showLoading({
                title: '加载中'
            })
            wx.cloud.callFunction({
                name: "update",
                data: {
                    action: "pushdzid",
                    id: this.data.id
                }
            }).then(res => {
                console.log(res);
                wx.hideLoading({
                    success: (res) => {},
                })
                if (res.result.data.stats.updated === 1) {
                    wx.showToast({
                        title: '点赞成功',
                    })
                    this.setData({
                        dianzanbtn: true,
                        dianzanshu: this.data.dianzanshu + 1
                    })
                }
            })
        } else {
            wx.showLoading({
                title: '加载中'
            })
            wx.cloud.callFunction({
                name: "update",
                data: {
                    action: "pulldzid",
                    id: this.data.id
                }
            }).then(res => {
                console.log(res);
                wx.hideLoading({
                    success: (res) => {},
                })
                if (res.result.data.stats.updated === 1) {
                    wx.showToast({
                        title: '取消成功',
                    })
                    this.setData({
                        dianzanbtn: false,
                        dianzanshu: this.data.dianzanshu - 1
                    })
                }
            })

        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        DB.collection('news').where({
            _id: options.id,
            sc_openid: _.in([wx.getStorageSync('openid')])
        }).get().then((res) => {
            console.log(res)
            if (res.data.length > 0) {
                this.setData({
                    shoucangbtn: true
                })
            }
        })

        DB.collection('news').where({
            _id: options.id,
            dz_openid: _.in([wx.getStorageSync('openid')])
        }).get().then((res) => {
            console.log(res)
            if (res.data.length > 0) {
                this.setData({
                    dianzanbtn: true
                })
            }
        })




        DB.collection('news').doc(options.id)
            .get()
            .then((res) => {
                console.log(res)
                this.setData({
                    orderItem: res.data,
                    id: options.id,
                    dianzanshu: res.data.dz_openid.length,
                    shoucangshu: res.data.sc_openid.length
                })
            })


    },
    bbb() {
        DB.collection('news').where({
            _id: this.data.id,
            sc_openid: _.in([wx.getStorageSync('openid')])
        }).get().then((res) => {
            console.log(res)
            if (res.data.length > 0) {
                this.setData({
                    shoucangbtn: true
                })
            }
        })
    },

    ccc() {
        DB.collection('news').doc(this.data.id)
            .get()
            .then((res) => {
                console.log(res)
                this.setData({
                    orderItem: res.data,
                    dianzanshu: res.data.dz_openid.length,
                    shoucangshu: res.data.sc_openid.length
                })
            })

    },
    ddd() {
        DB.collection('news').where({
            _id: this.data.id,
            dz_openid: _.in([wx.getStorageSync('openid')])
        }).get().then((res) => {
            console.log(res)
            if (res.data.length > 0) {
                this.setData({
                    dianzanbtn: true
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},


    InputFocus(e) {
        console.log(e);
        this.setData({
            InputBottom: e.detail.height,
        })
    },
    InputBlur(e) {
        console.log(e);
        this.setData({
            InputBottom: 0
        })
    },

    shuru(e) {
        console.log(e);
        this.content = e.detail.value
    },
    //发送文字消息
    send(e) {
        let that = this
        console.log(e);
        wx.showLoading({
            title: '发送中',
        })

        let content = {}

        content.msg = this.content
        content.nickName = wx.getStorageSync('user').nickName
        content.userPhoto = wx.getStorageSync('user').userPhoto

        wx.cloud.callFunction({
            name: "update",
            data: {
                id: this.data.id,
                action: "updatemovies",
                content: content,
            }
        }).then(res => {
            console.log(res);
            if (res.result.data.stats.updated === 1) {
                that.bbb()
                that.ccc()
                that.ddd()
                that.setData({
                    value: ''
                })
            }
            wx.hideLoading({
                success: (res) => {},
            })
        })
    },



})